package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.CouponAdapter;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.Coupon;
import com.hcinfos.gsoo.enity.ViewSuppDetail;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.json.CouponJson;
import com.hcinfos.gsoo.util.LogUtil;

public class ShopCouponActivity extends Activity implements OnClickListener {

	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;

	private ListView couponListView;
	private int page = 1;
	private int rows = 10;
	private Context context;

	private View moreView;
	private ProgressBar pageBar;
	private TextView loading_tv;
	private RelativeLayout load_rl;

	private final int GET_COUPON_SUCCESS = 0x1120;
	private final int GET_COUPON_FAIL = GET_COUPON_SUCCESS + 1;
	private final int GET_COUPON_EMPTY = GET_COUPON_SUCCESS + 2;

	private List<Coupon> list;
	private CouponAdapter adapter;

	private RelativeLayout progress_layout;
	private LinearLayout loading_dialog_content_layout;
	private RelativeLayout loading_staus_rl;
	private TextView status_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		setContentView(R.layout.activity_shop_coupon);
		context = this;
		initRes();
		initData();
	}

	private void initRes() {
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		left_ib.setOnClickListener(this);
		reg_tv.setOnClickListener(this);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("优惠信息");
		couponListView = (ListView) findViewById(R.id.couponList);

		list = new ArrayList<Coupon>();
		adapter = new CouponAdapter(this, list);
		moreView = getLayoutInflater().inflate(R.layout.loader_more, null);
		loading_tv = (TextView) moreView.findViewById(R.id.load_tv);
		load_rl = (RelativeLayout) moreView.findViewById(R.id.load_rl);
		load_rl.setOnClickListener(this);
		pageBar = (ProgressBar) moreView.findViewById(R.id.pb);
		couponListView.addFooterView(moreView);
		couponListView.setAdapter(adapter);
		couponListView.setOnItemClickListener(itemOnClickListener);

		progress_layout = (RelativeLayout) findViewById(R.id.progress_layout);
		progress_layout.setOnClickListener(this);
		progress_layout.setVisibility(View.VISIBLE);
		loading_dialog_content_layout = (LinearLayout) findViewById(R.id.loading_dialog_content_layout);
		loading_staus_rl = (RelativeLayout) findViewById(R.id.loading_staus_rl);
		loading_staus_rl.setVisibility(View.GONE);// 内容提示
		loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条
		status_tv = (TextView) findViewById(R.id.status_tv);
	}

	OnItemClickListener itemOnClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {

			if (position >= list.size())
				return;
			Coupon coupon = list.get(position);
			int type = 0;
			if (coupon.getActType().equals("coupon")) {
				type = 0;
			} else {
				type = 1;
			}
			Intent intent = new Intent(ShopCouponActivity.this,
					CouponDetailActivity.class);
			intent.putExtra("coupon", coupon);
			intent.putExtra("type", type);
			startActivity(intent);
		}
	};
	private ViewSuppDetail detail;
	private int total = 0;

	private void initData() {

		detail = (ViewSuppDetail) getIntent()
				.getSerializableExtra("shopDetail");

		if (detail == null)
			return;
		center_tv.setText(detail.getSuppName() + " 优惠");
		obtainCoupon();

	}

	private void obtainCoupon() {
		new Thread(new Runnable() {

			@Override
			public void run() {

				try {
					String result = HttpClientUtil.getFromWebByHttpClient(
							context,
							Urls.COUPON_BY_SHOPID + "suppId=" + detail.getId()
									+ "&rows=" + rows + "&page=" + page);
					if (result == null) {
						Message msg = handler.obtainMessage();
						msg.what = GET_COUPON_FAIL;
						handler.sendMessage(msg);
					} else {
						List<Coupon> resultList = CouponJson
								.getCouponListByJson(result);
						if (resultList.size() == 0) {
							Message msg = handler.obtainMessage();
							msg.what = GET_COUPON_EMPTY;
							handler.sendMessage(msg);
						} else {
							total = CouponJson.total;
							list.addAll(resultList);
							Message msg = handler.obtainMessage();
							msg.what = GET_COUPON_SUCCESS;
							handler.sendMessage(msg);
						}
						LogUtil.d("wxf", "size:" + list.size());
					}

					LogUtil.d("wxf", "result:" + result);
				} catch (Exception e) {
					Message msg = handler.obtainMessage();
					msg.what = GET_COUPON_FAIL;
					handler.sendMessage(msg);
					LogUtil.d("wxf", "obtainCoupon:" + e.toString());
				}
			}
		}).start();
	}

	Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GET_COUPON_SUCCESS:
				progress_layout.setVisibility(View.GONE);
				couponListView.setVisibility(View.VISIBLE);
				adapter.setDataChange(list);
				LogUtil.d("wxf", "total:" + total + "list.size()" + list.size());
				if (total > rows && total != list.size()) {
					loading_tv.setVisibility(View.VISIBLE);
					pageBar.setVisibility(View.GONE);
				} else {
					couponListView.removeFooterView(moreView);
				}
				break;
			case GET_COUPON_FAIL:
				status_tv.setText("加载失败,点击我重新加载");
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				break;
			case GET_COUPON_EMPTY:
				status_tv.setText("暂无优惠信息");
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				progress_layout.setClickable(false);
				break;
			}
		};
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.progress_layout:
			loading_staus_rl.setVisibility(View.GONE);// 内容提示
			loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条
			obtainCoupon();
			break;
		case R.id.load_rl:
			page += 1;
			pageBar.setVisibility(View.VISIBLE);
			obtainCoupon();
			break;
		}
	}

}
